package com.zou.lotterylucky.utils

import android.annotation.TargetApi
import android.content.ContentValues.TAG
import android.content.Context
import android.os.Build
import android.os.LocaleList
import android.util.Log
import com.elvishew.xlog.XLog
import java.util.*


object LangUtil {
    fun attachBaseContext(context: Context, language: String): Context {
        Log.d(TAG, "attachBaseContext: " + Build.VERSION.SDK_INT)
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            updateResources(context, language)
        } else {
            context
        }
    }

    @TargetApi(Build.VERSION_CODES.N)
    private fun updateResources(context: Context, language: String): Context {
        val resources = context.resources
        val locale = getLocaleByLanguage(language)
        XLog.e(locale.language)
        val configuration = resources.configuration
        configuration.setLocale(locale)
        configuration.setLocales(LocaleList(locale))
        return context.createConfigurationContext(configuration)
    }

    fun getLocaleByLanguage(s: String): Locale {
        return when (s) {
            "0" -> Locale("es", "CR")
            "1" -> Locale.CHINESE
            "2" -> Locale.ENGLISH
            else -> Locale.CHINESE
        }
    }
}